#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/16 0016 16:39
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : yilong2
import requests
import time
from multiprocessing.managers import BaseManager
import queue
import re
import pandas as pd


class Yi(BaseManager):
    pass


def run(size, code):
    header = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': '1602',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        # 'Cookie': 'CookieGuid=30e17c22-e527-4431-86fe-c7b80e7a179d; SessionGuid=900c31d0-90fa-4014-b577-bfb269e06603; Esid=e5a12095-eb05-45bb-860f-7117360e6c13; semid=ppzqbaidu; outerFrom=ppzqbaidu; com.eLong.CommonService.OrderFromCookieInfo=Status=1&Orderfromtype=5&Isusefparam=0&Pkid=50793&Parentid=3150&Coefficient=0.0&Makecomefrom=0&Cookiesdays=0&Savecookies=0&Priority=9000; fv=pcweb; newjava1=03eb38321f47362048ca1230c8f58cac; s_eVar44=ppzqbaidu; businessLine=train; H5Channel=h5frweb%2CNormal; H5CookieId=90328bf9-54a1-4b09-b859-e5e96f6ea620; _fid=jch3u1dc-f520-408b-8836-4f544709e5f8; JSESSIONID=8AB101B56738529769C8591C4C086CC7; ShHotel=CityID=0101&CityNameCN=%E5%8C%97%E4%BA%AC%E5%B8%82&CityName=%E5%8C%97%E4%BA%AC%E5%B8%82&OutDate=2018-01-18&CityNameEN=beijing&InDate=2018-01-17; ADHOC_MEMBERSHIP_CLIENT_ID1.0=78a8a6e3-7cbf-4368-903f-df700f80cfb6; s_cc=true; _RF1=1.199.192.25; _RSG=oUqi4SHXLw3JfioIIIhmAA; _RDG=2851d530c2f0762d9732cc4d938b2d0474; _RGUID=3e958517-04b3-47b2-a2b0-ad2492caca53; s_visit=1; s_sq=elongcom%3D%2526pid%253Dhotel.elong.com%25252Fbeijing%2526pidt%253D1%2526oid%253Djavascript%25253Avoid(0)%2526ot%253DA',
        'Host': 'hotel.elong.com',
        'Origin': 'http://hotel.elong.com',
        'Referer': 'http://hotel.elong.com/beijing/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    params = {
        'code': code,
        'listRequest.areaID': '',
        'listRequest.bookingChannel': '5',
        'listRequest.cardNo': '192928',
        'listRequest.checkInDate': '2018-01-17 00:00:00',
        'listRequest.checkOutDate': '2018-01-18 00:00:00',
        'listRequest.cityID': '0101',
        'listRequest.cityName': '北京市',
        'listRequest.customLevel': '11',
        'listRequest.distance': '20',
        'listRequest.endLat': '0',
        'listRequest.endLng': '0',
        'listRequest.facilityIds': '',
        'listRequest.highPrice': '0',
        'listRequest.hotelBrandIDs': '',
        'listRequest.isAdvanceSave': 'false',
        'listRequest.isAfterCouponPrice': 'true',
        'listRequest.isCoupon': 'false',
        'listRequest.isDebug': 'false',
        'listRequest.isLimitTime': 'false',
        'listRequest.isLogin': 'false',
        'listRequest.isMobileOnly': 'true',
        'listRequest.isNeed5Discount': 'true',
        'listRequest.isNeedNotContractedHotel': 'false',
        'listRequest.isNeedSimilarPrice': 'false',
        'listRequest.isReturnNoRoomHotel': 'true',
        'listRequest.isStaySave': 'false',
        'listRequest.isTrace': 'false',
        'listRequest.isUnionSite': 'false',
        'listRequest.keywords': '',
        'listRequest.keywordsType': '0',
        'listRequest.language': 'cn',
        'listRequest.listType': '0',
        'listRequest.lowPrice': '0',
        'listRequest.orderFromID': '50793',
        'listRequest.pageIndex': size,
        'listRequest.pageSize': '20',
        'listRequest.payMethod': '0',
        'listRequest.personOfRoom': '0',
        'listRequest.poiId': '0',
        'listRequest.promotionChannelCode': '0000',
        'listRequest.proxyID': 'ZD',
        'listRequest.rankType': '0',
        'listRequest.returnFilterItem': 'true',
        'listRequest.sellChannel': '1',
        'listRequest.seoHotelStar': '0',
        'listRequest.sortDirection': '1',
        'listRequest.sortMethod': '1',
        'listRequest.starLevels': '',
        'listRequest.startLat': '0',
        'listRequest.startLng': '0',
        'listRequest.taRecommend': 'false',
        'listRequest.themeIds': '',
        'listRequest.ctripToken': '3e958517-04b3-47b2-a2b0-ad2492caca53',
        'listRequest.elongToken': 'jch3u1dc-f520-408b-8836-4f544709e5f8'
    }
    url = "http://hotel.elong.com/ajax/list/asyncsearch"
    e = requests.post(url, data=params, headers=header)
    html = e.json()['value']['hotelListHtml']
    hotel_name = re.findall('target="_blank" title="(.*?)">', html)
    price = re.findall('<span class="h_pri_num ">(.*?)</span>', html)
    # detail=re.findall('data-hotelid="(.*?)" data-hotelname="(.*?)" data-hoteladdress="(.*?)" >',html)
    # print(hotel_name)
    # print(price)
    # print(detail)
    # print(list(map(lambda x:(hotel_name[x],price[x]),range(20))))
    if len(hotel_name) == 0:
        return -2
    try:
        new = list(map(lambda x: (hotel_name[x], price[x]), range(min(len(hotel_name), len(price)))))
        print(new)
        detail2 = pd.DataFrame(new, columns=['店名', '价格'])
        if size == 1:
            detail2.to_csv('a.csv', mode='a+', index=False, encoding='gbk')
        else:
            detail2.to_csv('a.csv', mode='a+', index=False, header=False, encoding='gbk')
        aa = pd.read_csv('a.csv', encoding='gbk', index_col=0, header=None)
        print(aa)
        print(len(aa))
    except:
        return -1
    return len(new)
    # 显示信息
    # print(detail2.loc[:4,:'2'])
    # print(pd.read_csv('a.csv',encoding='gbk',header=0,index_col=None))


if __name__ == '__main__':
    for size in range(2, 200):
        import random

        code = random.randint(9000000, 9999999)
        num = run(size, code)
        if num > 0:
            time.sleep(1)
            print((size - 1) * num, '-', size * num, 'is ok', size)
        else:
            print("done", num)
            break
        exit()
    print("dsaasd")
